/*
 * <h3>Title:wheel-parent系统</h3>
 * <p>Copyright (c) 2019, wnfbx@163.com. All rights reserved.</p>
 * @author wnfbx@163.com
 */
package com.wheel.autoconfigure.dynamicdatasource.loadbalancer;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <dt>Description:LoadBalanceStrategy</dt>
 * <dd>轮询均衡器</dd>
 *
 * @author wnfbx@163.com
 * @version 1.0 <i>2019/12/16 21:21</i>
 * @since jdk1.7+
 */
public class RoundRobinDataSourceLoadBalancer implements DataSourceLoadBalancer {
    /**
     * 负载均衡计数器
     */
    private static AtomicInteger index = new AtomicInteger(0);

    @Override
    public String determine(List<String> dataSourceKeys) {
        if (dataSourceKeys.size() == 1) {
            return dataSourceKeys.get(0);
        }
        int i = index.incrementAndGet();
        if (i == dataSourceKeys.size()) {
            index.compareAndSet(i, 0);
        }
        return dataSourceKeys.get(i);
    }
}
